package com.hxl.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxl.config.LogAnnotation;
import com.hxl.entity.CmfzBanner;
import com.hxl.entity.CmfzGuru;
import com.hxl.service.CmfzGuruService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * <p>
 * InnoDB free: 10240 kB 前端控制器
 * </p>
 *
 * @author hxl
 * @since 2020-08-18
 */
@RestController
@RequestMapping("guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cmfzGuruService;
    @RequestMapping("gurus")
    @LogAnnotation(content = "查看上师信息",type = "select")
    public Map<String,Object> selectBanners(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<>();
        Page<CmfzGuru> p = cmfzGuruService.selectGurus(page, limit);

        map.put("code",0);
        map.put("msg","");
        map.put("count",p.getTotal());
        map.put("data",p.getRecords());

        return map;
    }

    @RequestMapping("uploadGurus")
    @LogAnnotation(content = "批量上师信息",type = "insert")
    public Map<String,Object> uploadGurus(MultipartFile file) throws Exception {
        Map<String,Object> map = new HashMap<>();

        //将上传的文档放入输入流
        InputStream inputStream = file.getInputStream();

        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(0);
        importParams.setHeadRows(1);

        List<CmfzGuru> list = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);
        System.out.println(list.size());
        cmfzGuruService.saveBatch(list);
        for (CmfzGuru cmfzGuru : list) {
            System.out.println(cmfzGuru);
        }
        map.put("code",0);
        return map;
    }

    @RequestMapping("downGurus")
    @LogAnnotation(content = "批量下载上师信息",type = "download")
    public Map<String,Object> downGurus(@RequestParam(value = "ids") List<Integer> ids) throws IOException {
        Map<String,Object> map = new HashMap<>();
        List<CmfzGuru> cmfzGurus = (List<CmfzGuru>) cmfzGuruService.listByIds(ids);
        ExportParams exportParams = new ExportParams("上师列表", "gurus");
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class, cmfzGurus);
        Random random = new Random();
        String name = "test" + random.nextInt();
        sheets.write(new FileOutputStream("E://"+name+".xlsx"));
        map.put("code",0);
        return map;
    }
}
